package com.csyaonie.android;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.csyaonie.android.utils.ToastUtil;

public class ProgressBarActivity extends AppCompatActivity {

    private ProgressBar pb1;

    private Button pb_bt1;

    private Button progress_dialog_bt1;

    private Button progress_dialog_bt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar);
        pb1=findViewById(R.id.pb1);
        pb_bt1=findViewById(R.id.pb_bt1);
        progress_dialog_bt1=findViewById(R.id.progress_dialog_bt1);
        progress_dialog_bt2=findViewById(R.id.progress_dialog_bt2);

        pb1.setProgress(60);
        pb_bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0);
            }
        });

        progress_dialog_bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog=new ProgressDialog(ProgressBarActivity.this);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加载");
                progressDialog.show();
            }
        });

        progress_dialog_bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog progressDialog=new ProgressDialog(ProgressBarActivity.this);
                progressDialog.setTitle("下载");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog.dismiss();
                    }
                });
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });

    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(pb1.getProgress()<100){
                handler.postDelayed(runnable,300);
            }else{
                ToastUtil.showLong(ProgressBarActivity.this,"下载完成");
            }
        }
    };

    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            pb1.setProgress(pb1.getProgress()+5);
            handler.sendEmptyMessage(0);
        }
    };

}